import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

public class HttpServer {

    private int port;

    public HttpServer(int port) {
        this.port = port;
    }

    public void run() throws Exception {

        // 单线程模式
//        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
//        ServerBootstrap serverBootstrap = new ServerBootstrap();
//        serverBootstrap.group(bossGroup);

        // 非主从多线程模式
//        EventLoopGroup bossGroup = new NioEventLoopGroup();
//        ServerBootstrap serverBootstrap = new ServerBootstrap();
//        serverBootstrap.group(bossGroup);

        // 主从多线程模式
        EventLoopGroup bossGroup = new NioEventLoopGroup(3);
        EventLoopGroup workGroup = new NioEventLoopGroup(12);

        ServerBootstrap serverBootstrap = new ServerBootstrap();
        serverBootstrap.group(bossGroup,workGroup);

        serverBootstrap.channel(NioServerSocketChannel.class)
                .childHandler(new LoggingHandler(LogLevel.INFO)).childHandler(new HttpInitializer());


        Channel channel = serverBootstrap.bind(port).sync().channel();
        channel.closeFuture().sync();
    }

}
